/* 
 * Copyright 2005-2010 Samuel Mello
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; version 2 or later of the License.
 * 
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 * 
 */

/*
 * ConfigFrame.java
 *
 * Created on Dec 24, 2009, 1:03:12 AM
 */

package datasoul.config;

import java.awt.Component;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.util.ArrayList;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;

import com.sun.jna.Platform;

import datasoul.DatasoulMainForm;
import datasoul.render.OutputDevice;
import datasoul.util.ObjectManager;

/**
 *
 * @author samuel
 */
public class ConfigFrame extends javax.swing.JFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = -2986444321184109906L;
	private ConfigObj configObj;
    private ArrayList<Component> components;



    /** Creates new form ConfigFrame */
    public ConfigFrame() {
        initComponents();

        DatasoulMainForm.setDatasoulIcon(this);

        initDeviceMenu();

        components = new ArrayList<Component>();
        registerComponents();

        configObj = ConfigObj.getNextInstance();
        refreshScreenValues();

        setSize(600,650);
        updateHeight();

    }

    private void updateHeight(){

        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                setSize(getWidth(), btnSave.getY()+btnSave.getHeight()*2 + 10);
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel10 = new javax.swing.JPanel();
        btnStgloc = new javax.swing.JButton();
        txtStorageLoc = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        cbMonitorOutput = new javax.swing.JCheckBox();
        cbDetectMonitors = new javax.swing.JCheckBox();
        pnlMonitors = new javax.swing.JPanel();
        cbMainDevice = new javax.swing.JComboBox();
        jLabel28 = new javax.swing.JLabel();
        jLabel29 = new javax.swing.JLabel();
        cbMonitorDevice = new javax.swing.JComboBox();
        lblMonitorDisabled = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();
        btnDiscard = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        cbOnlineUpdate = new javax.swing.JCheckBox();
        cbOnlineStats = new javax.swing.JCheckBox();
        jTextArea1 = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        clockMode = new javax.swing.JComboBox();
        cbTrackDuration = new javax.swing.JCheckBox();
        cbAutoStartTimer = new javax.swing.JCheckBox();
        jLabel11 = new javax.swing.JLabel();
        txtSofficeLocation = new javax.swing.JTextField();
        btnSofficeLocation = new javax.swing.JButton();
        cbRemoteDisplays = new javax.swing.JCheckBox();
        cbRemoteAlerts = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("datasoul/internationalize"); // NOI18N
        setTitle(bundle.getString("DATASOUL CONFIGURATION")); // NOI18N

        jPanel10.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("DIRECTORY WHERE DATASOUL STORE ITS FILES"))); // NOI18N

        btnStgloc.setText(bundle.getString("CHANGE")); // NOI18N
        btnStgloc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStglocActionPerformed(evt);
            }
        });

        txtStorageLoc.setEditable(false);
        txtStorageLoc.setText("jTextField1");

        jLabel10.setText(bundle.getString("STORAGE DIRECTORY")); // NOI18N

        javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10);
        jPanel10.setLayout(jPanel10Layout);
        jPanel10Layout.setHorizontalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel10Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtStorageLoc, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnStgloc)
                .addContainerGap())
        );
        jPanel10Layout.setVerticalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel10)
                .addComponent(txtStorageLoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnStgloc))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("OUTPUT CONFIGURATION"))); // NOI18N

        cbMonitorOutput.setText(bundle.getString("ENABLE STAGE OUTPUT")); // NOI18N

        cbDetectMonitors.setText(bundle.getString("AUTOMATICALLY DETECT OUTPUTS")); // NOI18N
        cbDetectMonitors.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbDetectMonitorsActionPerformed(evt);
            }
        });

        cbMainDevice.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel28.setText(bundle.getString("MAIN OUTPUT")); // NOI18N

        jLabel29.setText(bundle.getString("STAGE OUTPUT")); // NOI18N

        cbMonitorDevice.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout pnlMonitorsLayout = new javax.swing.GroupLayout(pnlMonitors);
        pnlMonitors.setLayout(pnlMonitorsLayout);
        pnlMonitorsLayout.setHorizontalGroup(
            pnlMonitorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMonitorsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlMonitorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel28, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel29, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlMonitorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbMonitorDevice, 0, 444, Short.MAX_VALUE)
                    .addComponent(cbMainDevice, 0, 444, Short.MAX_VALUE)))
        );
        pnlMonitorsLayout.setVerticalGroup(
            pnlMonitorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMonitorsLayout.createSequentialGroup()
                .addGroup(pnlMonitorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbMainDevice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel28))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlMonitorsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbMonitorDevice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel29)))
        );

        lblMonitorDisabled.setFont(lblMonitorDisabled.getFont().deriveFont(lblMonitorDisabled.getFont().getSize()-2f));
        lblMonitorDisabled.setText(bundle.getString("(YOU NEED AT LEAST 2 DISPLAYS TO ENABLE STAGE OUTPUT)")); // NOI18N

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnlMonitors, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cbDetectMonitors)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(cbMonitorOutput)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblMonitorDisabled)))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbMonitorOutput)
                    .addComponent(lblMonitorDisabled))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbDetectMonitors)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlMonitors, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/datasoul/icons/v2/document-save.png"))); // NOI18N
        btnSave.setText(bundle.getString("SAVE AND CLOSE")); // NOI18N
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnDiscard.setIcon(new javax.swing.ImageIcon(getClass().getResource("/datasoul/icons/v2/window-close.png"))); // NOI18N
        btnDiscard.setText(bundle.getString("DISCARD AND CLOSE")); // NOI18N
        btnDiscard.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDiscardActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("ONLINE ACTIONS"))); // NOI18N

        cbOnlineUpdate.setText(bundle.getString("NOTIFY ME WHEN A NEW VERSION OF DATASOUL IS AVAILABLE")); // NOI18N

        cbOnlineStats.setText(bundle.getString("SEND ANONYMOUS USAGE STATISTICS")); // NOI18N

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setFont(new java.awt.Font("SansSerif", 1, 13));
        jTextArea1.setLineWrap(true);
        jTextArea1.setText(bundle.getString("IF YOU LIVE IN A COUNTRY WHERE CHRISTIANS ARE PERSECUTED AND DO NOT WISH TO RISK DETECTION YOU SHOULD NOT USE ANY OF THESE FUNCTIONS.")); // NOI18N
        jTextArea1.setWrapStyleWord(true);
        jTextArea1.setBorder(null);
        jTextArea1.setFocusable(false);
        jTextArea1.setOpaque(false);
        jTextArea1.setRequestFocusEnabled(false);
        jTextArea1.setVerifyInputWhenFocusTarget(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextArea1)
                    .addComponent(cbOnlineUpdate)
                    .addComponent(cbOnlineStats))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(cbOnlineUpdate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbOnlineStats)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextArea1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("CONFIGURE DATASOUL BEHAVIOR"))); // NOI18N

        jLabel9.setText(bundle.getString("CLOCK FORMAT")); // NOI18N

        clockMode.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        cbTrackDuration.setText(bundle.getString("MONITOR THE DURATION OF EACH SERVICE ITEM")); // NOI18N

        cbAutoStartTimer.setText(bundle.getString("START TIMER WHEN ITEM IS PRESENTED")); // NOI18N

        jLabel11.setText(bundle.getString("OPENOFFICE_LOCATION")); // NOI18N

        txtSofficeLocation.setEditable(false);
        txtSofficeLocation.setText("jTextField1");

        btnSofficeLocation.setText(bundle.getString("CHANGE")); // NOI18N
        btnSofficeLocation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSofficeLocationActionPerformed(evt);
            }
        });

        cbRemoteDisplays.setText(bundle.getString("ACCEPT REMOTE DISPLAYS")); // NOI18N

        cbRemoteAlerts.setText(bundle.getString("ACCEPT ALERTS FROM NETWORK")); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(cbAutoStartTimer))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addGap(43, 43, 43)
                        .addComponent(clockMode, 0, 430, Short.MAX_VALUE))
                    .addComponent(cbTrackDuration)
                    .addComponent(cbRemoteDisplays)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(cbRemoteAlerts, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtSofficeLocation, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSofficeLocation)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(clockMode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbTrackDuration)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbAutoStartTimer)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(txtSofficeLocation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSofficeLocation))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbRemoteDisplays)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbRemoteAlerts))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnSave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnDiscard))
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel10, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDiscard)
                    .addComponent(btnSave))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnStglocActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStglocActionPerformed
        JFileChooser fc = new JFileChooser();
        File dir = new File(txtStorageLoc.getText());
        fc.setCurrentDirectory(dir);
        fc.setDialogType(JFileChooser.OPEN_DIALOG);
        fc.setMultiSelectionEnabled(false);
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //        fc.setDialogTitle("Select directory");
        if(fc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION && fc.getSelectedFile().exists() ){
            txtStorageLoc.setText(fc.getSelectedFile().getAbsolutePath());
        }
    }//GEN-LAST:event_btnStglocActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        try{
            ObjectManager.getInstance().setBusyCursor();
            refreshObjectValues();
            configObj.save();
            JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("datasoul/internationalize").getString("CONFIGURATION WILL TAKE EFFECT NEXT TIME DATASOUL IS STARTED"), "Datasoul", JOptionPane.INFORMATION_MESSAGE);

        }finally{
            ObjectManager.getInstance().setDefaultCursor();
        }
        ObjectManager.getInstance().setConfigFrame(null);
        dispose();

    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnDiscardActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDiscardActionPerformed
        ObjectManager.getInstance().setConfigFrame(null);
        dispose();
    }//GEN-LAST:event_btnDiscardActionPerformed

    private void cbDetectMonitorsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbDetectMonitorsActionPerformed
        pnlMonitors.setVisible(!cbDetectMonitors.isSelected());
        updateHeight();
    }//GEN-LAST:event_cbDetectMonitorsActionPerformed

    private void btnSofficeLocationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSofficeLocationActionPerformed
        JFileChooser fc = new JFileChooser();
        File dir = new File(txtSofficeLocation.getText());
        fc.setCurrentDirectory(dir);
        fc.setDialogType(JFileChooser.OPEN_DIALOG);
        fc.setMultiSelectionEnabled(false);
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fc.setAcceptAllFileFilterUsed(false);
        fc.setFileFilter(new FileFilter(){

            @Override
            public boolean accept(File f) {

                if (Platform.isLinux()){
                    return (!f.isFile() || f.getName().equals("soffice"));
                }

                if (Platform.isWindows()){
                    return (!f.isFile() || f.getName().equalsIgnoreCase("soffice.exe"));
                }

                if (Platform.isMac()){
                    File f2 = new File(f.getAbsolutePath()+"/Contents/MacOS/soffice");
                    return f2.exists();
                }

                return false;
            }

            @Override
            public String getDescription() {
                return java.util.ResourceBundle.getBundle("datasoul/internationalize").getString("OPENOFFICE EXECUTABLE")+" (soffice, soffice.exe)";
            }

        });
        if(fc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION && fc.getSelectedFile().exists() ){
            if (Platform.isMac()){
                txtSofficeLocation.setText(fc.getSelectedFile().getAbsolutePath()+"/Contents/MacOS/soffice");
            } else {
                txtSofficeLocation.setText(fc.getSelectedFile().getAbsolutePath());
            }
        }
    }//GEN-LAST:event_btnSofficeLocationActionPerformed

    private void registerComponents(){
        registerComponent(cbMonitorOutput,"MonitorOutputIdx");
        registerComponent(cbDetectMonitors, "DetectMonitorsIdx");
        registerComponent(txtStorageLoc, "StorageLoc");

        registerComponent(clockMode,"ClockMode");
        clockMode.removeAllItems();
        for (int i=0; i<ConfigObj.CLOCKMODE_TABLE.length; i++){
            clockMode.addItem(ConfigObj.CLOCKMODE_TABLE[i]);
        }

        registerComponent(cbMainDevice, "MainOutputDevice");
        registerComponent(cbMonitorDevice, "MonitorOutputDevice");

        registerComponent(cbOnlineUpdate, "OnlineCheckUpdate");
        registerComponent(cbOnlineStats, "OnlineUsageStats");
        registerComponent(cbTrackDuration, "TrackDuration");

        registerComponent(cbAutoStartTimer, "AutoStartTimer");
        registerComponent(txtSofficeLocation, "SofficePath");

        registerComponent(cbRemoteDisplays, "AcceptRemoteDisplays");
        registerComponent(cbRemoteAlerts, "AcceptRemoteAlerts");
    }

    private void initDeviceMenu(){

        cbMainDevice.removeAllItems();
        cbMonitorDevice.removeAllItems();

        for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()){
            cbMainDevice.addItem(gd.getIDstring());
            cbMonitorDevice.addItem(gd.getIDstring());
        }

        cbMainDevice.addItem(java.util.ResourceBundle.getBundle("datasoul/internationalize").getString("NONE"));
        cbMonitorDevice.addItem(java.util.ResourceBundle.getBundle("datasoul/internationalize").getString("NONE"));
    }

    private void registerComponent(Component component, String string){
        components.add(component);
        component.setName(string);
    }

    private void setComponentValue(String string, Component component){
        if(component instanceof JCheckBox){
            if(string != null && string.compareToIgnoreCase("1")==0){
                ((JCheckBox)component).setSelected(true);
            }else{
                ((JCheckBox)component).setSelected(false);
            }
        }else if(component instanceof JComboBox){
            ((JComboBox)component).setSelectedItem(string);
        }else if(component instanceof JTextField){
            ((JTextField)component).setText(string);
        }
    }

    private String getComponentValue(Component component){
        if(component instanceof JCheckBox){
            if(((JCheckBox)component).isSelected()){
                return "1";
            }else{
                return "0";
            }
        }else if(component instanceof JComboBox){
            return (String)((JComboBox)component).getSelectedItem();
        }else if(component instanceof JTextField){
            return ((JTextField)component).getText();
        }
        return "";
    }

    private void refreshScreenValues(){
        for(Component component:components){
            try {
                Object o = configObj.getClass().getMethod("get"+component.getName()).invoke(configObj);
                setComponentValue( o.toString(), component);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        pnlMonitors.setVisible(!cbDetectMonitors.isSelected());

        boolean allowMonitor = OutputDevice.isMonitorAllowed();
        lblMonitorDisabled.setVisible(!allowMonitor);
        cbMonitorDevice.setEnabled(allowMonitor);
        cbMonitorOutput.setEnabled(allowMonitor);

    }

    private void refreshObjectValues(){
        for(Component component:components){
            try {
                configObj.getClass().getMethod("set"+component.getName(), String.class).invoke(configObj, getComponentValue(component));
            } catch (Exception ex) {
                System.err.println("Setting "+component.getName()+":");
                ex.printStackTrace();
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDiscard;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSofficeLocation;
    private javax.swing.JButton btnStgloc;
    private javax.swing.JCheckBox cbAutoStartTimer;
    private javax.swing.JCheckBox cbDetectMonitors;
    private javax.swing.JComboBox cbMainDevice;
    private javax.swing.JComboBox cbMonitorDevice;
    private javax.swing.JCheckBox cbMonitorOutput;
    private javax.swing.JCheckBox cbOnlineStats;
    private javax.swing.JCheckBox cbOnlineUpdate;
    private javax.swing.JCheckBox cbRemoteAlerts;
    private javax.swing.JCheckBox cbRemoteDisplays;
    private javax.swing.JCheckBox cbTrackDuration;
    private javax.swing.JComboBox clockMode;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel29;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JLabel lblMonitorDisabled;
    private javax.swing.JPanel pnlMonitors;
    private javax.swing.JTextField txtSofficeLocation;
    private javax.swing.JTextField txtStorageLoc;
    // End of variables declaration//GEN-END:variables

}


